// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function getDate(str){
    var reg = new RegExp("\\D+");
    var strArray = str.split(reg);
    if (strArray.length > 2) {
        var year = strArray[0] * 1;
        if (year >= 70 && year <= 99) 
            year += 1900;
        if (year >= 0 && year <= 50) 
            year += 2000;
        var month = strArray[1] - 1;
        var day = strArray[2];
        
        var date = new Date(year, month, day);
        return date;
    }
    else {
        return null;
    }
}

function show_text_field(select_id, text_field_id, link){
    if (link.innerHTML.indexOf("选择") == 0) {
        $(select_id).show();
		//$(select_id).focus();
        $(text_field_id).value = "";
        $(text_field_id).hide();
        link.innerHTML = "输入";
    }
    else {
        $(select_id).value = "";
        $(select_id).hide();
        $(text_field_id).show();
		$(text_field_id).focus();
        link.innerHTML = "选择";
    }
}

function edit_vehicle_rent_record(ctrl) {
	var input = ctrl.getElementsByTagName("input")[0];
    var p = ctrl.getElementsByTagName("p")[0];
    
    var is_use = input.value.split("|")[0];
    var date = input.value.split("|")[1];
    
    if (is_use == "0") {
        ctrl.className = "vehicle_use";
        input.value = "1|" + date;
        p.innerHTML = "使用";
    }
    else {
        ctrl.className = "vehicle_unuse";
        input.value = "0|" + date;
        p.innerHTML = "未用";
    }
}

function during_changed(){
    var start_date = getDate($('tour_group_during_start').value);
    var end_date = getDate($('tour_group_during_end').value);
    if (start_date != null && end_date != null) {
		// 移除已经存在的用车记录
		var nodes = $('vehicle_rent_records_div').childNodes;
		for(var i = nodes.length - 1; i >= 0 ; i--) {
			$(nodes[i]).remove();
		}
        for (var date = start_date; date <= end_date; date.setDate(date.getDate() + 1)) {
            var div = document.createElement("div");
            div.title = date.getMonth() + 1 + "月" + date.getDate() + "日";
            div.className = "vehicle_use";
            
            var input1 = document.createElement("input");
            input1.name = "vehicle[vehicle_rent_record_days][]";
            input1.type = "hidden";
            input1.value = "1|" + date;
            
            var label_date = document.createElement("label");
            label_date.innerHTML = date.getDate();
            
            var p = document.createElement("p");
            p.innerHTML = "使用";
            
            div.appendChild(label_date);
            div.appendChild(input1);
            div.appendChild(p);
            
            div.onclick = function(){
                edit_vehicle_rent_record(this);
            };
            $('vehicle_rent_records_div').appendChild(div);
        }
    }
}

// 计算北京成本
function calc_bj_cost() {
	var cost = $F("lodging_lodging_cost") * 1;
	cost += $F("guide_involved_food_cost") * 1;
	cost += $F("guide_involved_attraction_ticket_cost") * 1;
	cost += $F("guide_involved_subsidy_for_guide") * 1;
	cost += $F("vehicle_travel_cost") * 1;
	if (!isNaN(cost)) {
		$("tour_group_bj_cost").value = cost;
		calc_total_cost();
	}
}

// 根据购物额计算流水
function calc_flow_cost(shopping_cost_ctrl){
    var flow_cost = shopping_cost_ctrl.value / 10;
    if (!isNaN(flow_cost)) {
        //var flow_cost_ctrl = shopping_cost_ctrl.parentNode.nextSibling.getElementsByTagName("input")[0];
        var flow_cost_ctrl = $(shopping_cost_ctrl.id.replace("_shopping_cost", "_flow_cost"));
        flow_cost_ctrl.value = flow_cost;
        calc_total_cost_by_flow_cost(flow_cost_ctrl);
    }
}

// 根据人头费计算人头费总计
function calc_people_cost_by_unit_price(unit_price_ctrl){
    var number_of_people_ctrl = $(unit_price_ctrl.id.replace("_unit_price", "_number_of_people"));
    var people_cost = unit_price_ctrl.value * number_of_people_ctrl.value;
    if (!isNaN(people_cost)) {
        var people_cost_ctrl = $(unit_price_ctrl.id.replace("_unit_price", "_people_cost"));
        people_cost_ctrl.value = people_cost;
        calc_total_cost_by_people_cost(people_cost_ctrl);
    }
}

// 根据人数计算人头费总计
function calc_people_cost_by_number_of_people(number_of_people_ctrl){
    var unit_price_ctrl = $(number_of_people_ctrl.id.replace("_number_of_people", "_unit_price"));
    var people_cost = unit_price_ctrl.value * number_of_people_ctrl.value;
    if (!isNaN(people_cost)) {
        var people_cost_ctrl = $(number_of_people_ctrl.id.replace("_number_of_people", "_people_cost"));
        people_cost_ctrl.value = people_cost;
        calc_total_cost_by_people_cost(people_cost_ctrl);
    }
}

// 根据流水计算小计
function calc_total_cost_by_flow_cost(flow_cost_ctrl){
    var people_cost_ctrl = $(flow_cost_ctrl.id.replace("_flow_cost", "_people_cost"));
    var total_cost = flow_cost_ctrl.value * 1 + people_cost_ctrl.value * 1;
    if (!isNaN(total_cost)) {
        var total_cost_ctrl = $(flow_cost_ctrl.id.replace("_flow_cost", "_total_cost"));
        total_cost_ctrl.value = total_cost;
        calc_shopping_total_cost();
    }
}

// 根据人头费总计计算小计
function calc_total_cost_by_people_cost(people_cost_ctrl){
    var flow_cost_ctrl = $(people_cost_ctrl.id.replace("_people_cost", "_flow_cost"));
    var total_cost = flow_cost_ctrl.value * 1 + people_cost_ctrl.value * 1;
    if (!isNaN(total_cost)) {
        var total_cost_ctrl = $(people_cost_ctrl.id.replace("_people_cost", "_total_cost"));
        total_cost_ctrl.value = total_cost;
        calc_shopping_total_cost();
    }
}

// 计算进店总计
function calc_shopping_total_cost(){
    var total_cost = 0;
    var i = 1;
    while (true) {
        var ctrl = $("shopping_record_" + i + "_total_cost");
        if (ctrl == null) 
            break;
        if (!isNaN(ctrl.value)) 
            total_cost += ctrl.value * 1;
        i++;
    }
    total_cost += $F("shopping_commission_from_guide") * 1;
    total_cost -= $F("shopping_return_to_customer") * 1;
    if (!isNaN(total_cost)) {
		$("shopping_total_cost").value = total_cost;
		calc_profit();
		calc_total_profit();
	}
}

// 计算外地小计
function calc_outer_expense_total_cost(){
    var total_cost = 0;
    total_cost += $F("outer_expense_lodging_cost") * 1;
    total_cost += $F("outer_expense_acceptance_cost") * 1;
	total_cost += $F("outer_expense_airticket_cost") * 1;
	total_cost += $F("outer_expense_trainticket_cost") * 1;
	total_cost += $F("outer_expense_boatticket_cost") * 1;
    if (!isNaN(total_cost)) {
		$("outer_expense_total_cost").value = total_cost;
		calc_total_cost();
	}
}

// 计算总成本
function calc_total_cost() {
	var cost = $F("tour_group_bj_cost") * 1 + $F("outer_expense_total_cost") * 1
	if (!isNaN(cost))
        $("tour_group_total_cost").value = cost;
		calc_profit();
}

// 计算做团利润
function calc_profit() {
	var profit = $F("tour_group_revenue") * 1;
	profit -= $F("tour_group_total_cost") * 1;
	if (!isNaN(profit)) {
		$("tour_group_profit").value = profit;
		calc_total_profit();
	}
}

// 计算总利润
function calc_total_profit() {
	var cost = $F("tour_group_profit") * 1 + $F("shopping_total_cost") * 1
	if (!isNaN(cost))
        $("tour_group_total_profit").value = cost;
}
